<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 容器 -->
<div id="root">
    <input placeholder="请输入" v-model="keyword">
    <button @click="score = 1">年龄升序</button>
    <button @click="score = 2">年龄降序</button>
    <button @click="score = 0">原顺序</button>
    <ul>
        <li v-for="person in filPersons" :key="person.id">
            {{ person.name }}----{{person.age}}
        </li>
    </ul>
</div>
<script type="text/javascript">
    new Vue({
        el: '#root',
        data: {
            keyword:'',
            score: 0,
            persons: [
                {id: 1, name: '马冬梅', age: 20},
                {id: 2, name: '周冬雨', age: 30},
                {id: 3, name: '周杰伦', age: 21},
                {id: 4, name: '温兆伦', age: 25},
            ],
            persons2: [],
        },
        computed:{
            filPersons(){
                const  arr=this.persons.filter((p)=>{
                    return p.name.indexOf(this.keyword)!== -1;
                })
                if(this.score){
                    arr.sort((a,b)=>{
                        return this.score===1?a.age-b.age:b.age-a.age;
                    });
                }
                return arr;
            }
        }
    })
</script>
</body>
</html>